<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/list.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
</head>

<body>
    <div class="bookContainer">
        <h2>图书列表展示</h2>
        <div class="navbar-justify-between">
            <div>
                <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
                <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <td>选择</td>
                    <td class="width100">图书ID</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>数量</td>
                    <td>定价</td>
                    <td>出版社</td>
                    <td>状态</td>
                    <td class="width200">操作</td>
                </tr>
            </thead>
            <tbody></tbody>
        </table>

        <div class="demo">
            <ul id="pageContainer" class="pagination justify-content-center"></ul>
        </div>

        <script>
            // 一加载页面，就向后端发送请求，得到图书列表
            getBookList();
            
            function getBookList() {
                $.ajax({
                    type: "get",
                    url: "/book/getListByPage" + location.search,
                    success: function(result) {

                        // 用户未登录、或者图书列表不支持访问的情况
                        if (result == null || result.code == "UNLOGIN") {
                            alert("用户未登录，请重新登录");
                            location.href = "login.html";
                        }

                        // 用户登录成功，页面虽然没有数据，但是支持访问的情况
                        if(result == null || result.data == null){
                            return;
                        }
                        
                        // 用户登录成功，图书列表页面有图书记录
                        var data = result.data;

                        // 通过循环遍历列表中的每一本书
                        var books = data.records;
                        var finalHTML = "";
                        
                        for (var book of books) {
                            finalHTML += '<tr>';
                            finalHTML += '<td><input type="checkbox" name="selectBook" value="' + book.id + '" id="selectBook" class="book-select"></td>';
                            finalHTML += '<td>' + book.id + '</td>';
                            finalHTML += '<td>' + book.bookName + '</td>';
                            finalHTML += '<td>' + book.author + '</td>';
                            finalHTML += '<td>' + book.count + '</td>';
                            finalHTML += '<td>' + book.price + '</td>';
                            finalHTML += '<td>' + book.publish + '</td>';
                            finalHTML += '<td>' + book.status + '</td>';
                            finalHTML += '<td>';
                            finalHTML += '<div class="op">';
                            finalHTML += '<a href="book_update.html?bookId=' + book.id + '">修改</a>';
                            finalHTML += '<a href="javascript:void(0)" onclick="deleteBook(' + book.id + ')">删除</a>';
                            finalHTML += '</div>';
                            finalHTML += '</td>';
                            finalHTML += '</tr>';
                        }
                        
                        $("tbody").html(finalHTML);

                        // 翻页信息
                        $("#pageContainer").jqPaginator({
                            totalCounts: result.data.total, // 总记录数
                            pageSize: 10,             // 每页的个数
                            visiblePages: 5,          // 可视页数
                            currentPage: result.data.pageRequest.currentPage, // 当前页码
                            first: '<li class="page-item"><a class="page-link">首页</a></li>',
                            prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页</a></li>',
                            next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页</a></li>',
                            last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页</a></li>',
                            page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}</a></li>',
                            // 页面初始化和页码点击时都会执行
                            onPageChange: function(page, type) {
                                if (type == "change") {
                                    location.href = "book_list.html?currentPage=" + page;
                                }
                            }
                        });
                    },
                    error: function(error){
                        if(error.status==401){
                            alert("用户未登录，请重新登录");
                            location.href = "login.html";
                        }
                    }
                });
            }
            
            function deleteBook(id) {
                var isDelete = confirm("确认删除?");
                if (isDelete) {
                    $.ajax({
                        type: "post",
                        url: "/book/deleteBook?bookId=" + id,
                        success: function(result){
                            console.log(result);
                            console.log(result.code);
                            console.log(result.data);
                            if(result.code=="SUCCESS"&&result.data==""){
                                // 删除成功
                                location.href = "book_list.html?currentPage=1";
                            }else{
                                // 删除失败
                                alert(result.errorMessage);
                            }
                        }
                    });
                }
            }
            
            function batchDelete() {
                var isDelete = confirm("确认批量删除?");
                if (isDelete) {
                    // 获取复选框的id
                    var ids = [];
                    $("input:checkbox[name='selectBook']:checked").each(function() {
                        ids.push($(this).val());
                    });
                    console.log(ids);
                    $.ajax({
                       type: "post",
                       url: "/book/batchDelete?ids="+ids,
                       success: function(result){
                            if(result.code=="SUCCESS"&&result.data==true){
                                // 后端返回 true ，表示批量删除图书成功， 这时候我们是希望页面跳转一下的
                                location.href = "book_list.html?currentPage=1";
                            }else{
                                // 后端返回 false
                                alert("批量删除图书失败");
                            }
                       }

                    });
                }
            }
        </script>
    </div>
</body>

</html>